﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Text.RegularExpressions;
using System.Reflection;
using FinPlusWCFConnData;
using FinPlusCommon;
using u = FinPlusUtility.Utility;

namespace FinPlusComponents
{
    public class FinPlusConnectService : FinPlusComponent
    {
        public override string[] TestStates { get { return new string[] { "TestString", "IsValid" }; } }
        public override IFunctions Adaptor { get { return _conn.GetFunctions(_serviceName); } }
        public override string TestString { get { return Adaptor.ToString(); } }

        private enum Params { Env, Group, Query };
        private readonly FinPlusServiceConn _conn = new FinPlusServiceConn();
        private readonly bool _isTransposed = false;
        private readonly string _serviceName, _serviceKey, _keyFormat, _parms;

        //construct
        public FinPlusConnectService(string serviceName, string serviceType, string parms, string keyFormat)
        {
            _serviceKey = FinPlusServiceConn.GetServiceKey(_serviceName = serviceName, _parms = parms, _keyFormat = keyFormat);
            base.Config = serviceName;
            
            var regx = Regex.Match(parms, @"group,(?<Group>.*?)\;");
            if (!regx.Groups[Params.Group.ToString()].Success) throw new Exception(string.Format("Service type {0} name {1} has params format problem", serviceType, serviceName));
            var group = regx.Groups[Params.Group.ToString()].Value.ToString();

            regx = Regex.Match(parms, @"env,(?<Env>.*?)\;");
            if (!regx.Groups[Params.Env.ToString()].Success) throw new Exception(string.Format("Service type {0} name {1} has params format problem", serviceType, serviceName));
            base.Env = u.EnumParse<u.Env>(regx.Groups[Params.Env.ToString()].Value.ToString(), u.Env.unknown);

            _conn.Connect(Env, Config, User, _serviceKey, serviceType, _parms.TrimEnd(';'), _isTransposed, false);

            IsValid = true;
        }

        public FinPlusConnectService(string serviceName, string serviceType,  string keyFormat, string xmlPath, string node)
        {
            _serviceKey = FinPlusServiceConn.GetServiceKey(_serviceName = serviceName, _parms = string.Empty, _keyFormat = keyFormat);

            if (xmlPath == "root")
            {
                var assemblyPath = Assembly.GetEntryAssembly().Location;
                xmlPath = assemblyPath.ToLower().Replace(".exe", ".client");
            }

            _conn.Connect(Env, Config, User, serviceType, _serviceKey, xmlPath, node, _isTransposed);
            IsValid = true;
        }

        //common control interface 
        public override void Dispose()
        {
            _conn.Dispose(_serviceKey);
        }
    }
}


























































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus